package com.maslyk.invitation.entitybean;

import java.util.Comparator;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.maslyk.invitation.audit.ObjectName;
import com.maslyk.invitation.audit.PropertyLabel;
import com.maslyk.invitation.entitybean.abstrct.GenericEntity;

/**
 * 
 * @author kris
 */
@Entity
@Table(name = "comment")
@ObjectName(label="Komentarz")
public class Comment implements GenericEntity
{
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Basic(optional = false)
	@Column(name = "id")
	private Integer id;
	@JoinColumn(name = "orderID", referencedColumnName = "id")
	@ManyToOne(optional = false, fetch = FetchType.LAZY)
	private Order order;
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, targetEntity = Salesman.class)
	@JoinColumn(name = "salesmanID")
	private Salesman salesmanID;
	@Basic(optional = false)
	@Column(name = "registrationDate")
	@Temporal(TemporalType.TIMESTAMP)
	@PropertyLabel(label="Data rejestracji")
	private Date registrationDate;
	@Column(name = "note")
	@PropertyLabel(label="Treść")
	private String note;

	public Comment()
	{
	}

	public Comment(Integer id)
	{
		this.id = id;
	}

	public Comment(Integer id, Order orderID, Salesman salesmanID,
			Date registrationDate, String note)
	{
		this.id = id;
		this.order = orderID;
		this.salesmanID = salesmanID;
		this.registrationDate = registrationDate;
		this.note = note;
	}

	public Integer getId()
	{
		return id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	public Order getOrder()
	{
		return order;
	}

	public void setOrder(Order orderID)
	{
		this.order = orderID;
	}

	public Salesman getSalesmanID()
	{
		return salesmanID;
	}

	public void setSalesmanID(Salesman salesmanID)
	{
		this.salesmanID = salesmanID;
	}

	public Date getRegistrationDate()
	{
		return registrationDate;
	}

	public void setRegistrationDate(Date registrationDate)
	{
		this.registrationDate = registrationDate;
	}

	public String getNote()
	{
		return note;
	}

	public void setNote(String note)
	{
		if(note == null)
			return;
		
		this.note = com.maslyk.invitation.utils.StringUtils.replaceUnknownChars(note);
	}

	@Override
	public int hashCode()
	{
		int hash = 0;
		hash += (id != null ? id.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object)
	{
		if (!(object instanceof Comment))
			return false;
		
		Comment other = (Comment) object;
		if (this.registrationDate.equals(other.getRegistrationDate()) &&
			this.salesmanID.equals(other.getSalesmanID()))
			return true;
		
		return false;
	}

	@Override
	public String toString()
	{
		return "com.maslyk.invitation.entitybean.Comment[id=" + id + "]";
	}

	public static String getBeanName()
	{
		return "commentBean";
	}
	
	public String getKey() {
		return getOrder().getNumber();
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Comment c =(Comment) super.clone();
		c.setId(null);
		return c;
	}
	
	public boolean isInserted() {
		return id != null;
	}
	
	public static class CommentComparator implements Comparator<Comment> {

		public int compare(Comment o1, Comment o2) {
			return o2.getRegistrationDate().compareTo(o1.getRegistrationDate());
		}
	}
}
